const rds = require('ali-rds');
 
const db = rds({
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  password: 'root',
  database: 'db_auto'
});

async function getIp(){
   let rows =  await db.query('SELECT * FROM ip_records where lastdo_time >=? and lastdo_time< ?',[tenDateAgo(),new Date()]);
   return rows;
}

async function getDoUrls(ip){
    let rows =  await db.query('SELECT * FROM down_records where ip = ? and  down_time >=? and down_time< ?',[ip,tenDateAgo(),new Date()]);    
    return rows;
}

async function getUrls(ip){
  let rows =  await db.query('SELECT * FROM tasks ');    
  return rows;
}

function tenDateAgo(){
  let curTime = new Date().getTime();
  let date = new Date(curTime-1000*60*10);
  return date;
}

async function recordsTask ({ ip,url}){
  await db.insert('ip_records', {ip,lastdo_time: new Date()});
  await db.insert('down_records', {ip,url,down_time: new Date()});
  // await db.update('tasks', {last_down_time: new Date()},{
  //   where: {
  //     url: url
  //   }
  // });
}
module.exports = {
    db,
    getIp,
    getUrls,
    recordsTask,
    getDoUrls,
}